package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.modular.business.entity.Category;
import cn.stylefeng.guns.modular.business.model.result.CategoryResultView;
import cn.stylefeng.guns.modular.business.model.result.CategoryTypeView;
import cn.stylefeng.guns.modular.business.service.CategoryService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/gunsApi/category")
public class CategoryApi extends BaseController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation("获取等级分类")
    @GetMapping("/level/categories")
    public ResponseData get(Long categoryId) {
        Category category = categoryService.getCategoryById(categoryId);
        List<CategoryResultView> categoryResultViews = categoryService.getCategories(category.getPid(), true);
        return ResponseData.success(categoryResultViews);
    }

    @ApiOperation("首页获取所有分类类型下的分类")
    @GetMapping("/categories")
    public ResponseData getCategoriesAll() {
        Map<String, List<CategoryResultView>> map = new HashMap<>();
        List<CategoryTypeView> categoriesType = categoryService.getCategoriesType();
        categoriesType.forEach(categoryTypeView -> map.put(categoryTypeView.getCategoryType(), categoryService.getCategories(categoryTypeView.getCategoryId(), false)));
        return ResponseData.success(map);
    }


    /**
     * 获取分类类型
     *
     * @return List<CategoryTypeView>
     */
    @ApiOperation("获取分类类型")
    @GetMapping("/categories/type")
    public ResponseData getCategoriesType() {
        return ResponseData.success(categoryService.getCategoriesType());
    }

    @ApiOperation("初始化分类")
    @GetMapping("/init")
    public void initCategoriesToRedis() {
        categoryService.initCacheCategories();
    }

}


